<template>
	<view>
		<view class="diy-product" :style="{
				background: itemData.style.bgcolor,
				paddingLeft: itemData.style.paddingLeft * 2 + 'rpx',
				paddingRight: itemData.style.paddingLeft * 2 + 'rpx',
				paddingTop: itemData.style.paddingTop * 2 + 'rpx',
				paddingBottom: itemData.style.paddingBottom * 2 + 'rpx'
			}">
			<view :class="['display__' + itemData.style.display]" :style="{
					background: itemData.style.background,
					borderTopLeftRadius: itemData.style.topRadio * 2 + 'rpx',
					borderTopRightRadius: itemData.style.topRadio * 2 + 'rpx',
					borderBottomLeftRadius: itemData.style.bottomRadio * 2 + 'rpx',
					borderBottomRightRadius: itemData.style.bottomRadio * 2 + 'rpx'
				}">
				<template v-if="itemData.style.display == 'list'">
					<view class="product-list" :class="['column__' + itemData.style.column]">
						<view class="product-item" v-for="(product, index) in itemData.data" :key="index">
							<!-- 单列商品 -->
							<template v-if="itemData.style.column == 1">
								<view class="product-item-box">
									<!-- 商品图片 -->
									<view class="product-cover">
										<image lazy-load :src="product.image" mode="aspectFill"></image>
									</view>
									<view class="product-info">
										<!-- 商品名称 -->
										<view v-if="itemData.params.productName" class="product-title">
											<text>{{ product.product_name }}</text>
										</view>
										<!-- 商品卖点 -->
										<!-- <view v-if="itemData.params.sellingPoint" class="selling-point gray9">
											<text>{{ product.selling_point }}</text>
										</view> -->
										<!-- 商品销量 -->
										<!-- <view v-if="itemData.params.productSales" class="already-sale">
											<text>已售{{ product.product_sales }}件</text>
										</view> -->
										<!-- 商品价格 -->
										<view class="price d-s-c mt10" v-if="product.user_auth == '1'">
											<view v-if="itemData.params.productPrice"
												:style="{color: itemData.style.product_price_color}">
												<text>¥</text>
												<text class="">{{ product.product_price }}</text>
											</view>
											<view class="ml10 gray9 text-d-line f28"
												v-if="itemData.params.linePrice && product.line_price > 0"
												:style="{color: itemData.style.line_price_color}">
												<text>¥</text>
												<text>{{ product.line_price }}</text>
											</view>
										</view>
									</view>
								</view>
							</template>

							<!-- 两列三列 -->
							<template v-else>
								<view class="product-cover"  @click="gotoDetail(product.product_id)">
									<image lazy-load :src="product.image" mode="aspectFill"></image>
								</view>
								<view class="product-info">
									<view v-if="itemData.params.productName == 1"
										:style="{color: itemData.style.product_name_color}" class="product-title"  @click="gotoDetail(product.product_id)">
										{{ product.product_name }}
									</view>
									<view class="price d-s-c" v-if="product.user_auth == '1'">
										<view v-if="itemData.params.productPrice == 1 && product.unit_price > 0"
											:style="{color: itemData.style.product_price_color}" >
											<text class="f22">¥</text>
											<text class="" style="font-weight: bold;font-size: 36rpx;">{{ product.unit_price }}/{{ product.unit }}</text>
										</view>

										<view class="ml20 gray9 text-d-line"
											:style="{color: itemData.style.line_price_color}"
											v-if="itemData.params.linePrice == 1 && product.line_price > 0">
<!-- 											<text class="f22">¥</text>
											{{ product.line_price }} -->
										</view>
										<view class="flex-container-sen">
											<!-- &nbsp;&nbsp;&nbsp;&nbsp; -->
<!-- 											<view v-if="product.cart_num == 0">
												<image lazy-load class="senyimiao_icon" src="/static/icon/reduce-gray.png"></image>
											</view>
											<view v-else> -->
<!-- 											<view>
												<image lazy-load class="senyimiao_icon" src="/static/icon/reduce.png" @click="reduceCarts(product)"></image>
											</view> -->
											<!-- &nbsp;&nbsp; -->
<!-- 											<view class="text-wrap-sen">
												{{product.cart_num}}
											</view> -->
												<image lazy-load class="senyimiao_icon" src="/static/icon/加号.png" @click="addCarts(product,index)"></image>
												<view class="badge" v-if="product.cart_num > 0">{{product.cart_num}}</view>
												<view class="badge_none" v-else></view>
												<!-- 提示数字 -->
										</view>
											
									</view>
								</view>
							</template>
						</view>
					</view>
				</template>
				<template v-else>
					<scroll-view :scroll-top="scrollTop" scroll-x="true">
						<view class="product-list" :class="['column__' + itemData.style.column]">
							<view v-for="(product, index) in itemData.data" :key="index"
								@click="gotoDetail(product.product_id)">
								<view class="product-item">
									<view class="product-cover">
										<image lazy-load :src="product.image" mode="aspectFill"></image>
									</view>
									<view class="product-info">
										<view v-if="itemData.params.productName == 1"
											:style="{color: itemData.style.product_name_color}" class="product-title">
											{{ product.product_name }}
										</view>
										<view class="price d-s-c f12" v-if="product.user_auth == '1'">
											<view v-if="itemData.params.productPrice == 1"
												:style="{color: itemData.style.product_price_color}">
												<text>¥</text>
												<text class="">{{ product.product_price }}</text>
											</view>
											<view class="ml10 gray9 text-d-line"
												:style="{color: itemData.style.line_price_color}"
												v-if="itemData.params.linePrice == 1 && product.line_price > 0">
												¥{{ product.line_price }}
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</template>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {

			};
		},
		props: ['itemData', 'user_auth'],
		created() {
			/*获取数据*/
			let self = this;
		},

		methods: {
			scroll(e) {},
			addCarts(product, index) {
				
				if(product.spec_type == 10) {
					// 单规格
					this.addSpec(product.product_id, 'Single',index);
				} else {
					// 多规格
					this.addSpec(product.product_id, 'Many',index);
				}
				
				this.itemData.data[index].cart_num++;
			},
			reduceCarts(e) {
				if(e.spec_type == 10) {
					var spec_sku_id_ss = 0;
				} else {
					spec_sku_id_ss = -1;
				}
				this._post(
					'order.cart/sub', {
						product_id: e.product_id,
						spec_sku_id: spec_sku_id_ss,
					},
					function(res) {
						if(res.msg === 0) {
							setTimeout(()=>{
								uni.showToast({
									title: '商品数量为0',
									icon: 'none',
									mask: false,
									duration: 1000
								});
							},10);
						} else {
							setTimeout(()=>{
								uni.showToast({
									title: '移除购物车成功',
									icon: 'none',
									mask: false,
									duration: 1000
								});
							},10);
						}
				
					}
				);
				var spec_sku_id_ss = 0;
			},

			/*跳转产品详情*/
			gotoDetail(e) {
				let url = '/pages/product/detail/detail?product_id=' + e;
				this.gotoPage(url);
			},
			addSpec(product_id, type, index) {
				var spec_sku_id_send = 0;
				if(type == 'Many') {
					spec_sku_id_send = -1;
				}
				this._post(
					"order.cart/add", {
						product_id: product_id,
						total_num: 1,
						spec_sku_id: spec_sku_id_send,
					},
					function(res) {
						if(res.code == 1) {
							setTimeout(()=>{
								uni.showToast({
									title: '添加购物车成功',
									icon: 'none',
									mask: false,
									duration: 1000
								});
							},10);
						}

						
					}
				);
			},

		}
	};
</script>

<style>
	.badge {
	  position: relative;
	  top: -10px;
	  right: 10px;
	  background-color: #FF3B30; /* 红色背景 */
	  color: white;
	  border-radius: 50%;
	  width: 16px;
	  height: 16px;
	  line-height: 16px;
	  text-align: center;
	  font-size: 12px;
	  font-weight: bold;
	  /* box-shadow: 0 1px 3px rgba(0,0,0,0.2); */
	}
	.badge_none {
	  position: relative;
	  top: -10px;
	  right: 10px;
	  background-color: #FF3B30; /* 红色背景 */
	  color: white;
	  border-radius: 50%;
	  width: 16px;
	  height: 16px;
	  line-height: 16px;
	  text-align: center;
	  font-size: 12px;
	  font-weight: bold;
	  opacity: 0;
	  /* box-shadow: 0 1px 3px rgba(0,0,0,0.2); */
	}
	.text-wrap-sen {
		margin: 0 3rpx;
		width: 40rpx;
		height: 46rpx;
		background: #F4F4F4;
		border-radius: 10rpx;
		font-size: 24rpx;
		text-align: center;
		line-height: 46rpx;
	}
	.flex-container-sen {
	  display: flex;
	  flex-direction: row; /* 横向排列 */
	  align-items: center; /* 垂直居中对齐 */
	  justify-content: space-between; /* 元素之间平均分布 */
	  margin-left: auto;
	}
	.senyimiao_icon {
		width: 60rpx;
		height: 60rpx;
	}
	.diy-product {
		/* padding: 0 20rpx; */
	}

	.diy-product .product-list.column__1 {
		padding: 0 20rpx;
	}

	.diy-product .product-list.column__1 .product-item {
		padding: 20rpx 0;
		border-bottom: 1px solid #eee;
		background: #ffffff;
		width: 100%;
	}

	.diy-product .product-list.column__1 .product-item-box {
		display: flex;
		justify-content: flex-start;
		align-items: stretch;
		box-sizing: border-box;
	}

	.diy-product .product-list .product-cover image {
		width: 100%;
		height: 100%;
	}

	.diy-product .product-list.column__1 .product-cover {
		width: 240rpx;
		height: 240rpx;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.diy-product .product-list.column__1 .product-info {
		margin: 20rpx 20rpx 20rpx 30rpx;
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.diy-product .product-list .product-info .already-sale text {
		padding: 4rpx 8rpx;
		border-radius: 8rpx;
		background: #f2f2f7;
		color: #999;
	}

	.diy-product .product-list .product-title {
		display: -webkit-box;
		font-size: 26rpx;
		font-weight: bold;
		overflow: hidden;
		word-wrap: break-word;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.diy-product .product-list.column__1 .product-title {
		max-height: 80rpx;
		line-height: 40rpx;
	}

	.diy-product .product-list.column__1 .selling-point {
		display: -webkit-box;
		overflow: hidden;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		line-height: 30rpx;
		max-height: 60rpx;
	}

	.diy-product .product-list.column__1 .already-sale {
		margin-top: 10rpx;
	}

	.diy-product .product-list.column__2 .product-title {
		/* height: 80rpx; */
		line-height: 40rpx;
		margin-top: 20rpx;
		font-size: 28rpx;
		margin-bottom: 20rpx;
		font-weight: bold;
	}

	.diy-product .product-list.column__3 .product-title {
		margin-top: 20rpx;
		margin-left: 10rpx;
		margin-right: 10rpx;
		height: 72rpx;
		line-height: 36rpx;
		font-size: 30rpx;
	}

	.diy-product .product-list.column__2 .product-info {
		padding: 0 24rpx;
	}

	.diy-product .product-list .price {
		font-size: 34rpx;
	}

	.diy-product .product-list.column__2 .price {
		margin-top: 10rpx;
		font-size: 30rpx;
	}

	.diy-product .product-list.column__3 .price {
		margin-top: 10rpx;
		padding: 0 10rpx;
		font-size: 28rpx;
	}

	.diy-product .product-list.column__3 .price .text-d-line {
		font-size: 22rpx;
	}

	.diy-product .product-list.column__2,
	.diy-product .product-list.column__3 {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
	}

	.diy-product .column__2 .product-item {
		width: 345rpx;
		margin-right: 20rpx;
		margin-bottom: 20rpx;
		padding-bottom: 20rpx;
		overflow: hidden;
		background: #ffffff;
		border-radius: 12rpx;
	}

	.diy-product .display__list .column__2 .product-item:nth-child(2n + 0) {
		margin-right: 0;
	}

	.diy-product .column__2 .product-cover {
		width: 345rpx;
		height: 345rpx;
		border-radius: 12rpx;
		margin: 0 auto;
	}

	.diy-product .column__3 .product-item {
		width: 230rpx;
		margin-bottom: 10rpx;
		margin-right: 10rpx;
		padding-bottom: 10rpx;
		border-radius: 8rpx;
		overflow: hidden;
		background: #ffffff;
		box-shadow: 0 0 8rpx rgba(0, 0, 0, 0.1);
	}

	.diy-product .display__list .column__3 .product-item:nth-child(3n + 0) {
		margin-right: 0;
	}

	.diy-product .column__3 .product-cover {
		width: 230rpx;
		height: 230rpx;
	}

	.diy-product .display__slide .product-list {
		flex-wrap: nowrap;
	}

	.diy-product .display__slide .column__2 .product-item {
		width: 300rpx;
	}

	.diy-product .display__slide .column__3 .product-item {
		width: 200rpx;
	}
</style>